package com.ruixin.mvc.parser;

import com.ruixin.annotation.Parser;
import com.ruixin.base.BaseParser;
import com.ruixin.base.exception.BaseException;
import com.ruixin.base.util.BeanUtil;
import com.ruixin.mvc.annotation.RequestMapping;
import com.ruixin.mvc.config.ApplicationContextWrapper;
import com.ruixin.mvc.config.MethodMapping;
import com.ruixin.mvc.config.RequestType;
import com.ruixin.mvc.config.WebMapping;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

@Parser(annotation = RequestMapping.class)
public class RequestMappingParser extends BaseParser<ApplicationContextWrapper> {
    
    public RequestMappingParser(ApplicationContextWrapper context, Class<? extends Annotation> type) {
        super(context, type);
    }

    @Override
    public void parseMethod(Object bean, Method method) throws BaseException {
        super.parseMethod(bean, method);
        RequestMapping annotation = BeanUtil.getAnnotation(method, RequestMapping.class);
        String[] value = annotation.value();
        boolean jsonResult = annotation.jsonResult();
        RequestType[] type = annotation.type();
        MethodMapping methodMapping = new MethodMapping(value,type,jsonResult,method);
        WebMapping webMapping = this.context.getWebMapping(method.getDeclaringClass());
        if(webMapping.isJsonResult()){ //如果Web 或者 RestWeb 中 jsonResult=true,则取代methodMapping配置
            methodMapping.setJsonResult(true);
        }
        webMapping.setMethodMapping(method,methodMapping);
    }
}
